package com.dzm.demo.myshop.web.admin.service.impl;

import com.dzm.demo.myshop.commons.dto.BaseResult;
import com.dzm.demo.myshop.commons.dto.PageInfo;
import com.dzm.demo.myshop.commons.validator.BeanValidator;
import com.dzm.demo.myshop.domain.TbContent;
import com.dzm.demo.myshop.web.admin.abstracts.AbstractsBaseServiceImpl;
import com.dzm.demo.myshop.web.admin.dao.TbContentDao;
import com.dzm.demo.myshop.web.admin.service.TbContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class TbContentServiceImpl extends AbstractsBaseServiceImpl<TbContent,TbContentDao> implements TbContentService {

    @Override
    @Transactional(readOnly = false)
    public BaseResult save(TbContent tbContent) {

        String validator = BeanValidator.validator(tbContent);
        if (validator != null){
            return BaseResult.fail(validator);
        }
        else {
            tbContent.setUpdated(new Date());

            //新增内容
            if (tbContent.getId() == null){
                tbContent.setCreated(new Date());
                dao.insert(tbContent);
            }
            //编辑内容
            else {
                update(tbContent);
            }
        }

        return BaseResult.success("保存内容信息成功！");
    }

    @Override
    @Transactional(readOnly = false)
    public void deleteByCategoryId(String[] categoryIds) {
        dao.deleteByCategoryId(categoryIds);
    }
}
